package cn.codeforfun.controller;

import cn.codeforfun.entity.Student;
import cn.codeforfun.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * Created by Administrator on 2016/9/22.
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public String page() {
        return "index";
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Collection<Student> list() {
        return studentService.findAll();
    }


    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public void save(@RequestBody Student student) {
        studentService.save(student);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public void update(@RequestBody Student student) {
        studentService.save(student);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Student get(@PathVariable("id") Integer id) {
        return studentService.get(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable("id") Integer id) {
        studentService.delete(id);
    }
}
